//
//  BJMineArticleViewController.m
//  BowJoy
//
//  Created by  on 2024/5/21.
//

#import "BJMineArticleViewController.h"
#import "BJMineArticleListTableViewCell.h"
#import "BJShowEmptyView.h"
@interface BJMineArticleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *bgGroundView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSMutableArray<BJAnchorWallModelData *> *records;
@property (nonatomic,strong)BJShowEmptyView  *showEmptyView;

@end

@implementation BJMineArticleViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.naviTitle = @"My Articles";

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"BJMineArticleListTableViewCell" bundle:nil] forCellReuseIdentifier:@"BJMineArticleListTableViewCell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    NSArray *dataQueryList = [[BJPushFMDBManger sharedManager] queryAllData];
    if (dataQueryList.count>0) {
        self.records = [[NSMutableArray alloc]initWithArray:dataQueryList];
        [self.tableView reloadData];
    }else{
       
        self.tableView.addChild(self.showEmptyView);
        [self.tableView reloadData];

    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BJMineArticleListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BJMineArticleListTableViewCell"];
    if (cell == nil){
        cell = [[BJMineArticleListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BJMineArticleListTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BJAnchorWallModelData *model = self.records[indexPath.row];
    cell.model = model;
    kWeakSelf(weakSelf)

    cell.moreBtn.onClick(^{
        [ASHUD showASLoading];

        [[BJPushFMDBManger sharedManager] deleteDataWithUserId:model.userId];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [ASHUD hideHUD];
            [weakSelf.records removeObjectAtIndex:indexPath.row];
            [weakSelf.tableView reloadData];
            [ASHUD showWithText:@"Delete Successfully！" afterDelay:1];
        });

    });
    
    cell.followBtn.onClick(^{
        
        if (!model.isLike) {
            model.isLike = YES;
            model.followNum ++;
            [weakSelf.tableView reloadData];

        }else{
            model.isLike = NO;
            model.followNum --;
            [weakSelf.tableView reloadData];
           
        }
        
        
    });

    return  cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

-(BJShowEmptyView *)showEmptyView{
    if(!_showEmptyView){
        _showEmptyView = [[BJShowEmptyView alloc]initWithFrame:CGRectMake(0, kNavBarHeight, self.tableView.width, self.tableView.height)];
    
    }
    return _showEmptyView;
}


@end
